package com.ray.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
public class LoggerAspect {

    /**
     * 面向切面
     */

    /**
     * 切所有方法
     *
     */
    @Before("execution(public int com.ray.utils.impl.CalImpl.*(..))")
    public void before(JoinPoint joinPoint){
        //获取方法名
        String name = joinPoint.getSignature().getName();
        //获取参数
        Object args[] = joinPoint.getArgs();
        System.out.println(name+"的参数是"+ Arrays.toString(args));
    }

    /**
     * 方法执行后
     */
    @After("execution(public int com.ray.utils.impl.CalImpl.*(..))")
    public void after(JoinPoint joinPoint){
        //获取方法名
        String name = joinPoint.getSignature().getName();
        System.out.println(name+"方法执行完毕");
    }

    /**
     * 返回参数
     */
    @AfterReturning(value = "execution(public int com.ray.utils.impl.CalImpl.*(..))",returning = "result")
    public void afterReturning(JoinPoint joinPoint,Object result){
        //获取方法名
        String name = joinPoint.getSignature().getName();
        System.out.println(name+"方法的结果是："+result);
    }


    /**
     * 异常
     */
    @AfterThrowing(value = "execution(public int com.ray.utils.impl.CalImpl.*(..))",throwing = "exception")
    public void afterThrowing(JoinPoint joinPoint,Exception exception){
        //获取方法名
        String name = joinPoint.getSignature().getName();
        System.out.println(name+"方法抛出异常："+exception);
    }
}
